try:
  import vte
except:
  vte = False
  
import pygtk
pygtk.require('2.0')
import gtk, gobject
import os

class phennywrapper:
    
    def __init__(self, args):
                
        self.term = vte.Terminal()
        self.term.connect("child-exited", lambda term: gtk.main_quit())
        self.term.fork_command()
        self.term.feed_child('cd %s\n' % os.path.dirname(args[0]))
        self.term.feed_child('python %s\n' % ' '.join(args))
        self.term.set_sensitive(False)

        self.window = gtk.Window()
        self.window.add(self.term)
        self.window.connect('delete-event', lambda window, event: gtk.main_quit())
        self.window.show_all()
        self.window.set_deletable(False)
        
        gobject.io_add_watch(sys.stdin, gobject.IO_IN, self.interpreter)

    def interpreter(self, stdin, flag):
        line = stdin.readline()[:-1]
        if not line:
            return True
        
        if line == 'show':
            self.window.present()
        elif line == 'hide':
            self.window.hide()
        elif line == 'quit':
            exit(0)
    
        return True
        
    def main(self):
        gtk.main()
        

if __name__ == '__main__':
    import sys
    if vte:
        pw = phennywrapper(sys.argv[1:])
        pw.main()
